<?php

namespace app\store\controller\member;

use app\store\controller\BaseController;
use app\store\service\StoreUserGradeService;
use think\exception\ValidateException;
use think\facade\View;
use app\store\model\UserGrade;

class GradeController extends BaseController
{
    public function index()
    {   

        $keyword = $this->request->param('keyword','');

        $list = StoreUserGradeService::getStoreUserGradeList($keyword);

        return View::fetch('',['list' => $list]);
    }

    public function create()
    {
        View::engine()->layout(false);
        return View::fetch();
    }


    public function edit()
    {
        $id = $this->request->param('id/d');

        $row = StoreUserGradeService::findStoreUserGrade($id);

        View::engine()->layout(false);
        return View::fetch('',['row' => $row]);
    }


    public function save()
    {
        $param = $this->request->param();

        try {
            validate('StoreUserGradeValidate')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $param['store_id'] = session('store_id');

        UserGrade::create($param);

        return json(['code' => 1, 'msg' => '保存成功!']);
    }


    public function update()
    {
        $param = $this->request->param();

        try {
            validate('StoreUserGradeValidate.edit')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }
        // print_r($param);exit;
        UserGrade::update($param);

        return json(['code' => 1, 'msg' => '更新成功!']);
    }


    public function switch()
    {
        $param = $this->request->param();

        try {
            validate('StoreUserGradeValidate.switch')->check($param);
        }catch(ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        UserGrade::update($param);

        return json(['code' => 1, 'msg' => '更新成功!']);
    }


    public function delete()
    {
        $id = $this->request->param('id/d');

        $row = UserGrade::destroy($id);

        return  $row ? json(['code' => 1, 'msg' => '删除成功!']) : json(['code' => 0, 'msg' => '删除失败!']);
    }

    
}